﻿namespace WebPacker.Tests
{
	using System.Collections.Generic;
	using System.IO;

	public static class TestHelper
	{
		public static string GetFullPath(string relativePath)
		{
			return Path.Combine(Directory.GetCurrentDirectory(), relativePath);
		}

		public static string GetSamplePath(string file, string category)
		{
			return Path.Combine(GetSampleFolder(category), file);
		}

		public static string GetSampleFolder(string category)
		{
			return GetFullPath(Path.Combine("samples", "temp", category));
		}

		public static string[] PrepareSampleFiles(string[] files, string category)
		{
			var samples = GetFullPath("samples");
			var temp = Path.Combine(samples, "temp");
			var tempCategory = Path.Combine(temp, category);
			var paths = new List<string>();

			if (!Directory.Exists(temp))
			{
				Directory.CreateDirectory(temp);
			}

			if (!Directory.Exists(tempCategory))
			{
				Directory.CreateDirectory(tempCategory);
			}
			else
			{
				foreach (var file in Directory.GetFiles(tempCategory))
				{
					File.Delete(file);
				}
			}

			foreach (var file in files)
			{
				var source = Path.Combine(samples, file);
				var dest = TestHelper.GetFullPath(Path.Combine(tempCategory, Path.GetFileName(file)));
				File.Copy(source, dest, true);
				paths.Add(dest);
			}

			return paths.ToArray();
		}

		public static string GetFileUri(string filePath)
		{
			var path = filePath;

			if (!Path.IsPathRooted(filePath))
			{
				path = GetFullPath(filePath);
			}

			return string.Format("file://{0}", path.Replace("\\", "/"));
		}
	}
}
